package com.xnyzc.lhy.mis.service.impl.cms.agreement;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.entity.Qw;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.penum.EAgreementType;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.param.cms.agreement.AgreementParam;
import com.xnyzc.lhy.mis.entity.result.withdrawalrules.CmsWithdrawalRules;
import com.xnyzc.lhy.mis.entity.system.OaSysAgreement;
import com.xnyzc.lhy.mis.mapper.system.OaSysAgreementMapper;
import com.xnyzc.lhy.mis.service.cms.agreement.AgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 协议相关
 * </p>
 *
 * @author wangshuo
 * @since 2019-09-09
 */
@Service
public class AgreementServiceImpl implements AgreementService {

    @Autowired
    private OaSysAgreementMapper oaSysAgreementMapper;

    /**
     * 用户协议
     *
     * @return
     */
    @Override
    public Rv getUserAgreement() {
        QueryWrapper<OaSysAgreement> agreementQueryWrapper = new QueryWrapper<>();
        agreementQueryWrapper.eq(OaSysAgreement.TYPE, EAgreementType.USER_AGREEMENT.getValue());
        List<OaSysAgreement> agreementList = oaSysAgreementMapper.selectList(agreementQueryWrapper);
        AgreementParam results = new AgreementParam();
        if (!agreementList.isEmpty()) {
            OaSysAgreement agreement = agreementList.get(0);
            results.setAgreementId(agreement.getAgreementId());
            results.setContent(agreement.getContent());
        }
        return Rv.wrap(EErrorCode.success, results);
    }

    /**
     * 编辑用户协议
     *
     * @return
     */
    @Override
    public Rv getUpdateUserAgreement(AgreementParam agreementParam) {
        Long userId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(agreementParam.getAgreementId())) {
            OaSysAgreement agreement = new OaSysAgreement();
            agreement.setType(EAgreementType.USER_AGREEMENT.getValue());
            agreement.setUpdateTime(new Date());
            agreement.setUpdateUser(userId);
            agreement.setContent(agreementParam.getContent());
            agreement.setCreateTime(new Date());
            agreement.setCreateUser(userId);
            agreement.setTag(tag);
            oaSysAgreementMapper.insert(agreement);
        } else {
            // 修改
            agreementUpdate(userId, agreementParam);
        }
        return Rv.wrap(EErrorCode.updateSuccess);
    }

    /**
     * 充值协议
     *
     * @return
     */
    @Override
    public Rv getRechargeAgreement() {
        QueryWrapper<OaSysAgreement> agreementQueryWrapper = new QueryWrapper<>();
        agreementQueryWrapper.eq(OaSysAgreement.TYPE, EAgreementType.RECHARGE_AGREEMENT.getValue());
        List<OaSysAgreement> agreementList = oaSysAgreementMapper.selectList(agreementQueryWrapper);
        AgreementParam results = new AgreementParam();
        if (!agreementList.isEmpty()) {
            OaSysAgreement agreement = agreementList.get(0);
            results.setAgreementId(agreement.getAgreementId());
            results.setContent(agreement.getContent());
        }
        return Rv.wrap(EErrorCode.success, results);
    }

    /**
     * 编辑充值协议
     *
     * @return
     */
    @Override
    public Rv getUpdateRechargeAgreement(AgreementParam agreementParam) {
        Long userId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(agreementParam.getAgreementId())) {
            OaSysAgreement agreement = new OaSysAgreement();
            agreement.setType(EAgreementType.RECHARGE_AGREEMENT.getValue());
            agreement.setUpdateTime(new Date());
            agreement.setUpdateUser(userId);
            agreement.setContent(agreementParam.getContent());
            agreement.setCreateTime(new Date());
            agreement.setCreateUser(userId);
            agreement.setTag(tag);
            oaSysAgreementMapper.insert(agreement);
        } else {
            // 修改
            agreementUpdate(userId, agreementParam);
        }
        return Rv.wrap(EErrorCode.updateSuccess);
    }

    @Override
    public Rv getAgreementByType(Integer type) {
        CmsWithdrawalRules cmsWithdrawalRules = oaSysAgreementMapper.getAgreementByType(type);
        return Rv.wrap(cmsWithdrawalRules);
    }

    public void agreementUpdate(Long userId, AgreementParam agreementParam) {
        OaSysAgreement agreement = oaSysAgreementMapper.selectById(agreementParam.getAgreementId());
        agreement.setUpdateUser(userId);
        agreement.setUpdateTime(new Date());
        agreement.setContent(agreementParam.getContent());
        oaSysAgreementMapper.updateById(agreement);
    }

    @Override
    public Rv updateAgreementByType(AgreementParam param) {
        Integer type = param.getType();
        if (type == null) {
            return Rv.error(EErrorCode.missingArg);
        }
        Long userId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        Date date = new Date();
        OaSysAgreement agreement = oaSysAgreementMapper.selectOne(Qw.create().eq(OaSysAgreement.TYPE, type));
        if (agreement == null) {
            //添加
            agreement = new OaSysAgreement();
            agreement.setContent(param.getContent());
            agreement.setType(param.getType());
            agreement.setTag(tag);
            agreement.setCreateUser(userId);
            agreement.setCreateTime(date);
            agreement.setUpdateTime(date);
            agreement.setUpdateUser(userId);

            int res = oaSysAgreementMapper.insert(agreement);
            if (res > 0) {
                return Rv.wrap();
            }
            return Rv.wrap(EErrorCode.applyAmountOperateFailure);
        } else {
            //修改
            agreement.setContent(param.getContent());
            agreement.setUpdateUser(userId);
            agreement.setUpdateTime(date);
            int res = oaSysAgreementMapper.updateById(agreement);
            if (res > 0) {
                return Rv.wrap();
            }
            return Rv.wrap(EErrorCode.applyAmountOperateFailure);
        }
    }
}
